// SCENARIO SCRIPT

beginscenarioscript;

variables;
short x,y,i,t,n,choice,to_squadron;
string str;

body;

beginstate LOAD_SCEN_STATE;
	set_flag(0,0,6);

	init_special_abil(0,"My X,Y coordinates",10);
	change_custom_abil_uses(0,0,1);
	change_custom_abil_uses(1,0,1);
	change_custom_abil_uses(2,0,1);
	change_custom_abil_uses(3,0,1);

	init_special_abil(1,"General command",14);
	change_custom_abil_uses(0,1,1);
	change_custom_abil_uses(1,1,1);
	change_custom_abil_uses(2,1,1);
	change_custom_abil_uses(3,1,1);
break;

beginstate START_SCEN_STATE;
break;

beginstate START_STATE;
break;

beginstate 10; // X,Y for party members
	change_custom_abil_uses(0,0,1);
	change_custom_abil_uses(1,0,1);
	change_custom_abil_uses(2,0,1);
	change_custom_abil_uses(3,0,1);
	i = who_used_custom_abil();
	x = char_loc_x(i);
	y = char_loc_y(i);
	clear_buffer();
	append_string("Your x,y coordinates are ");
	append_number(x);
	append_string(",");
	append_number(y);
	get_buffer_text(str);
	print_str_color(str,2);
break;

beginstate 11; // Commanding a soldier
	t = get_flag(0,10);
	if (get_attitude(t) >= 10) {
		if (get_flag(299,28) == 1) {
			reset_dialog();
			add_dialog_str(0,"Do you want to set this enemy as a target?",0);
			add_dialog_choice(0,"Yes.");
			add_dialog_choice(1,"No.");
			if (run_dialog(0) == 2)
				end();
			i = 6;
			while (i < 47) {
				if (char_ok(i)) {
					if (get_attitude(i) < 10 && get_flag(i,0) == 2) {
						set_target(i,t);
						set_flag(i,0,0);
					}
				}
				i = i + 1;
			}
			set_flag(299,28,0);
		}
		else {
			print_str_color("That character is an enemy.",3);
			print_str_color("You can't command hostile units.",3);
		}
		end();
	}
	if (what_group_in(t) > 0) {
		reset_dialog();
		add_dialog_str(0,"Do you want to apply your commands to this character's entire squadron?",0);
		add_dialog_choice(0,"Yes.");
		add_dialog_choice(1,"No.");
		add_dialog_choice(2,"See all members of this character's squadron.");
		choice = run_dialog(0);
		if (choice == 1)
			to_squadron = 1;
		if (choice == 3) {
			i = 6;
			while (i < 47) {
				if (char_ok(i)) {
					if (what_group_in(i) == what_group_in(t) && get_attitude(i) < 10) {
						set_total_visibility(1);
						force_view_center(char_loc_x(i),char_loc_y(i));
						force_instant_terrain_redraw();
						pause(10);
					}
				}
				i = i + 1;
			}
			set_total_visibility(0);
			force_view_center(char_loc_x(first_group_member(0)),char_loc_y(first_group_member(0)));
			force_instant_terrain_redraw();
			set_state_continue(11);
		}
	}
	reset_dialog();
	add_dialog_str(0,"What do you want this unit to do?",0);
	add_dialog_choice(0,"Follow a party member.");
	add_dialog_choice(1,"Stand still.");
	add_dialog_choice(2,"More options...");
	choice = run_dialog(0);
	if (choice == 1) {
		run_select_a_pc(0);
		i = get_selected_pc();
		if (to_squadron > 0)
			give_char_message(what_group_in(t) + 1000,i);
		else give_char_message(t,i);
	}
	if (choice == 2) {
		if (to_squadron > 0)
			give_char_message(what_group_in(t) + 1000,4);
		else give_char_message(t,4);
	}
	if (choice == 3) {
		reset_dialog();
		add_dialog_str(0,"What do you want this unit to do?",0);
		add_dialog_choice(0,"Bless allies.");
		add_dialog_choice(1,"Move to these coordinates.");
		add_dialog_choice(2,"More options...");
		choice = run_dialog(0);
		if (choice == 1) {
			if (to_squadron > 0)
				give_char_message(what_group_in(t) + 1000,5);
			else give_char_message(t,5);
			if (get_flag(0,4) == 0)
				message_dialog("This unit won't begin blessing units until the battle has started.","");
		}
		if (choice == 2)
			set_state_continue(12);
		if (choice == 3) {
			reset_dialog();
			add_dialog_str(0,"What do you want this unit to do?",0);
			add_dialog_choice(0,"Return to normal movement.");
			add_dialog_choice(1,"Wait for an enemy to approach.");
			add_dialog_choice(2,"More options...");
			choice = run_dialog(0);
			if (choice == 1) {
				if (to_squadron > 0)
					give_char_message(what_group_in(t) + 1000,100);
				else give_char_message(t,100);
			}
			if (choice == 2) {
				if (to_squadron > 0)
					give_char_message(what_group_in(t) + 1000,200);
				else give_char_message(t,200);
			}
			if (choice == 3) {
				reset_dialog();
				add_dialog_str(0,"What do you want this unit to do?",0);
				add_dialog_choice(0,"Attack an enemy.");
				add_dialog_choice(1,"Continue as before.");
				add_dialog_choice(2,"Try again. (Go through options again)");
				choice = run_dialog(0);
				if (choice == 1) {
					set_flag(299,28,1);
					if (to_squadron > 0) {
						i = 6;
						while (i < 47) {
							if (char_ok(i))
								if (what_group_in(i) == what_group_in(t))
									set_flag(i,0,2);
							i = i + 1;
						}
					}
					else set_flag(t,0,2);
					message_dialog("This character will continue to attack as normal until you pick a new target. To do that, use your banner item on an enemy unit. All characters that you have given this command will set that unit as their target.","");
				}
				if (choice == 3)
					set_state_continue(11);
			}
		}
	}
break;

beginstate 12;
	if (get_flag(0,12) == 0) {
		message_dialog("You will be asked for both an x and a y coordinate. Return a number from 1 to 47 for each.","");
		set_flag(0,12,1);
	}
	get_text_response("X coordinate?");
	n = 0;
	while (n <= 47) {
		clear_buffer();
		append_number(n);
		get_buffer_text(str);
		check_text_response_match(str);
		if (got_text_match() == 1)
			set_flag(0,1,n);
		n = n + 1;
	}
	if (get_flag(0,4) == 0) {
		if (get_flag(0,1) > 26) {
			set_flag(0,1,0);
			reset_dialog();
			add_dialog_str(0,"You can't move your troops that far forward before the battle. Do wish to try again or quit?",0);
			add_dialog_choice(0,"Try again.");
			add_dialog_choice(1,"Quit.");
			choice = run_dialog(0);
			if (choice == 1)
				set_state_continue(12);
			if (choice == 2)
				end();
		}
	}		
	get_text_response("Y coordinate?");
	n = 0;
	while (n <= 47) {
		clear_buffer();
		append_number(n);
		get_buffer_text(str);
		check_text_response_match(str);
		if (got_text_match() == 1) {
			set_flag(0,2,n);
		}
		n = n + 1;
	}
	if (get_flag(0,1) == 0 || get_flag(0,2) == 0) {
		reset_dialog();
		add_dialog_str(0,"One or both of your entries were not correct. Try again or quit?",0);
		add_dialog_choice(0,"Try again.");
		add_dialog_choice(1,"Quit.");
		choice = run_dialog(0);
		if (choice == 1) {
			set_state_continue(12);
		}
		if (choice == 2) {
			end();
		}
	}
	if (to_squadron > 0) {
		give_char_message(what_group_in(t) + 1000,6);
		i = 6;
		while (i < 47) {
			if (char_ok(i))
				if (what_group_in(i) == what_group_in(t))
					set_flag(i,0,1);
			i = i + 1;
		}
	}
	else {
		give_char_message(t,6);
		set_flag(t,0,1);
	}
break;

beginstate 13; // Using banner item
	if (get_attitude(who_is_custom_item_target()) < 10) {
		clear_buffer(); // Get the team moral and tell the player what it is
		append_string("This soldier's moral is ");
		if (get_flag(0,0) >= 8)
			append_string("high.");
		else if (get_flag(0,0) >= 4)
			append_string("average.");
		else if (get_flag(0,0) < 4)
			append_string("low.");
		get_buffer_text(str);
		print_str_color(str,2);
	}
	set_flag(0,10,who_is_custom_item_target());
	set_state_continue(11);
break;

beginstate 14; // Commanding everyone
	change_custom_abil_uses(0,1,1);
	change_custom_abil_uses(1,1,1);
	change_custom_abil_uses(2,1,1);
	change_custom_abil_uses(3,1,1);
	reset_dialog();
	add_dialog_str(0,"This ability allows you to give a command to the entire army. There are fewer commands, but it makes things go much faster.",0);
	add_dialog_str(1,"What do you want all units to do?",0);
	add_dialog_choice(0,"Stand still.");
	add_dialog_choice(1,"Wait for an enemy to approach.");
	add_dialog_choice(2,"More options...");
	choice = run_dialog(0);
	if (choice == 1) {
		i = 6;
		while (i < 47) {
			if (get_attitude(i) < 5) {
				give_char_message(i,4);
			}
			i = i + 1;
		}
	}
	if (choice == 2) {
		i = 6;
		while (i < 47) {
			if (get_attitude(i) < 5) {
				give_char_message(i,200);
			}
			i = i + 1;
		}
	}
	if (choice == 3) {
		reset_dialog();
		add_dialog_str(0,"What do you want all units to do?",0);
		add_dialog_choice(0,"Return to normal movement.");
		add_dialog_choice(1,"Continue as before.");
		choice = run_dialog(0);
		if (choice == 1) {
			i = 6;
			while (i < 47) {
				if (get_attitude(i) < 5) {
					give_char_message(i,100);
				}
				i = i + 1;
			}
		}
	}
break;